package co2.bean;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import co2.engine.DAOInterface;
import co2.pojos.Kelompok;
import co2.pojos.MstUser;
import co2.pojos.SoalLks;

@ManagedBean
@ViewScoped
public class HasilLksBean extends AbstractBean implements Serializable {
	private static final long serialVersionUID = 1L;

	@Autowired
	@Qualifier("daoI")
	private DAOInterface dao;
	
	private List<Kelompok> list;
	
	private Kelompok kelompok;
	
	@PostConstruct
	public void postCon(){
		list = dao.getKelompokJoinHasilLks();
		
	}
	
	public void delete(){
		if(kelompok!=null){
			SoalLks soal = dao.getSoalLksByKelompokId(kelompok.getId());
			if(soal!=null){
				try{
					dao.delete(SoalLks.class, soal.getId());
				}catch(Exception e){
					e.printStackTrace();
					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error delete soalLks",""));
				}
				
				try{
					dao.delete(Kelompok.class, kelompok.getId());
					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Deleted",""));
					
				}catch(Exception e){
					e.printStackTrace();
					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error delete parent object",""));
					
				}
			}
			
			list = dao.getKelompokJoinHasilLks();
		}
	}
	public void showDetail(){
		
		try{
			Map map = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
			map.put("kelompok", kelompok);
			
			String requestPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() ;
			FacesContext.getCurrentInstance().getExternalContext().redirect(requestPath+"/jsf/target/detail_lks.jsf");
		}catch(Exception e){
			e.printStackTrace();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error: "+e.getMessage(),""));
		}
	}

	public DAOInterface getDao() {
		return dao;
	}

	public void setDao(DAOInterface dao) {
		this.dao = dao;
	}

	public List<Kelompok> getList() {
		return list;
	}

	public void setList(List<Kelompok> list) {
		this.list = list;
	}

	public Kelompok getKelompok() {
		return kelompok;
	}

	public void setKelompok(Kelompok kelompok) {
		this.kelompok = kelompok;
	}

}
